import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import main

n = 150
xy = []  #存坐标
data = np.zeros((n, n))  # 生成全0矩阵
ims = []  # 存图片
fig = plt.figure()

for i in range(n):
    for j in range(n):
        xy.append([i, j])

for p in np.arange(0.01, 1, 0.01):

    for i in range(225):
        c = random.choice(xy)  # 随机挑一个坐标
        xy.remove(c)
        data[c[0], c[1]] = 1  # 将挑出的坐标值改为1

    s = main.Solution(data)
    res = s.bfs()  # 团簇坐标

    # 存最大团簇坐标
    cenx = []
    ceny = []
    # 存次大团簇坐标
    cenx1 = []
    ceny1 = []
    m = []
    m1 = []
    for i in res:
        m.append(len(i))
        m1.append(len(i))
    max_area = max(m)  # 最大团簇大小
    m1.remove(max_area)  # m1存除最大团簇以外的所有团簇大小

    # 找出最大团簇坐标
    for j in res:
        if len(j) == max_area:
            max_cluster_xy = j
            break
    for i in max_cluster_xy:
        cenx.append(i[0])
        ceny.append(i[1])

    # 当m1不为空时，也就是有次大团簇
    if m1 != []:
        for j in res:
            if len(j) == max(m1) and j != max_cluster_xy:
                second_max_cluster_xy = j
                break
        for i in second_max_cluster_xy:
            cenx1.append(i[0])
            ceny1.append(i[1])

        # 设置标题
        #plt.title('p=%.2f' % p)
        plt.title("max(r) and sub-large(b)")
        # 设置X轴标签
        plt.xlabel('X')
        # 设置Y轴标签
        plt.ylabel('Y')
        # 画散点图
        im = plt.scatter(cenx, ceny, c='red', marker='.').findobj()  # 最大团簇用红色表示
        im = im + plt.scatter(cenx1, ceny1, c='blue', marker='.').findobj()  # 次大团簇用蓝色表示
        plt.xlim(0, 150)
        plt.ylim(0, 150)

    # 当m1为空时，也就是没有次大团簇，只画出最大团簇即可
    else:
        #plt.title('p=%.2f' % p)
        plt.title("max(r) and sub-large(b)")
        # 设置X轴标签
        plt.xlabel('X')
        # 设置Y轴标签
        plt.ylabel('Y')
        # 画散点图
        im = plt.scatter(cenx, ceny, c='red', marker='.').findobj()
        plt.xlim(0, 150)
        plt.ylim(0, 150)

    ims.append(im)
# plt.close()
ani = animation.ArtistAnimation(fig, ims, interval=450, repeat_delay=3000)
ani.save("Growth.gif", writer='pillow')